/*
 * 创建日期 2005-5-30
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.guanghua.brick.html.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * @author Administrator
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class EncodingFilter implements Filter {
	
	private String encoding = null;

	public void init(FilterConfig filterConfig) throws ServletException {
		this.encoding = filterConfig.getInitParameter("encoding");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {		
		
		//编码设定
		if (request.getCharacterEncoding() == null && this.encoding != null) {
			request.setCharacterEncoding(this.encoding);
		}

		//获取URL
		String url = ((HttpServletRequest)request).getRequestURL().toString();
		
		//缓存设定
		if (url.endsWith(".jpg") || url.endsWith(".gif") || 
	        	url.endsWith(".png") || url.endsWith(".cur") ||
	        	url.endsWith(".ico") || url.endsWith(".bmp")) {
			//缓存有效时间1周
			//一天后IE会自动发送304去检查是否有更新，也可以使用IE的每次检查更新或者refresh方式强制更新
			((HttpServletResponse) response).setHeader("Cache-Control","max-age=604800");
		} else if (url.endsWith(".css") || url.endsWith(".js")) {
			//缓存有效时间1天
			//一天后IE会自动发送304去检查是否有更新，也可以使用IE的每次检查更新或者refresh方式强制更新
			((HttpServletResponse) response).setHeader("Cache-Control","max-age=86400");
		//其他的html, htm, jsp, do, xls, xml等，不进行缓存	
		} else {
			((HttpServletResponse) response).setHeader("Cache-Control","no-cache");
		}
		
		chain.doFilter(request, response);
	}				

	public void destroy() {
		this.encoding = null;
	}
}
